package com.tang.hawkmusic.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.tang.hawkmusic.R;
import com.tang.hawkmusic.adapter.ViewPagerAdapter;
import com.tang.hawkmusic.bean.PlaylistDTO;
import com.tang.hawkmusic.bean.UserInfo;
import com.tang.hawkmusic.fragment.DiscoverFragment;
import com.tang.hawkmusic.fragment.MineFragment;
import com.tang.hawkmusic.fragment.MiniPlayerFragment;
import com.tang.hawkmusic.fragment.PlayerFragment;
import com.tang.hawkmusic.fragment.PodcastFragment;
import com.tang.hawkmusic.fragment.SingFragment;
import com.tang.hawkmusic.fragment.VillageFragment;
import com.tang.hawkmusic.utill.ThemeUtill;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class BaseActivity extends AppCompatActivity {
    protected ArrayList<Fragment> fragments;
    public MiniPlayerFragment miniPlayerFragment; //主界面的音乐播放空间
    public PlayerFragment playerFragment;  //音乐控制播放界面
    public BottomNavigationView bottomNavigationView;
    public FrameLayout frameLayout;
    public BottomSheetBehavior<FrameLayout> bottomSheetBehavior;
    public ViewPager2 viewPager2;
    public BottomSheetBehavior.BottomSheetCallback bottomSheetCallback;
    public MainViewModel mainViewModel;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFragment();
        initMainSheet();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setMiniPlayerFragmentListener();
    }

    public void setMiniPlayerFragmentListener(){
        miniPlayerFragment.requireView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
            }
        });
    }
    //开始播放
    public void setPlayBegin(){
      //展开playfragment
        bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        //为了方便把数据给fragment
        playerFragment.setLocalMusics(mainViewModel.getLocalMusics());
        playerFragment.setPosition(mainViewModel.getCurrentItem());
        playerFragment.beginPlay();
    }
    //设置状态栏
    public void setStatusBar() {
        ThemeUtill.setAllowDrawUnderStatusBar(getWindow());
    }
    //初始化Viewpager2和BottomNavigationView
    public void initViewPager(){
        ViewPager2 viewPager=findViewById(R.id.mainViewPager);
        BottomNavigationView bottomNavigationView=findViewById(R.id.bottomNavigationView);
        //初始化fragment
        initFragments();
        //初始化ViewPager
        ViewPagerAdapter viewPagerAdapter=new ViewPagerAdapter(this,fragments);
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setOffscreenPageLimit(4);
        //初始化BottomNavigationView
        bottomNavigationView.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @SuppressLint("NonConstantResourceId")
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        switch (item.getItemId()){
                            case R.id.item_discover:
                                 viewPager.setCurrentItem(0,false);
                                 break;
                            case R.id.item_podcast:
                                viewPager.setCurrentItem(1,false);
                                break;
                            case R.id.item_mine:
                                viewPager.setCurrentItem(2,false);
                                break;
                            case R.id.item_sing:
                                viewPager.setCurrentItem(3,false);
                                break;
                            case R.id.item_village:
                                viewPager.setCurrentItem(4,false);
                                break;
                        }
                        return true;
                    }
                }
        );
        //初始化Viewpager
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                int item1= bottomNavigationView.getMenu().getItem(position).getItemId();
                int item2= bottomNavigationView.getSelectedItemId();
                if (item1!=item2) {
                    bottomNavigationView.getMenu().getItem(position).setChecked(true);
                    Log.d("tag Viewpager", "onPageScrolled: "+position);
                }
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                
            }
        });
    }

    //初始化fragment
    private void initFragments(){
        fragments=new ArrayList<>();
        DiscoverFragment discoverFragment=new DiscoverFragment();
        PodcastFragment podcastFragment=new PodcastFragment();
        MineFragment mineFragment=new MineFragment();
        SingFragment singFragment=new SingFragment();
        VillageFragment villageFragment=new VillageFragment();
        fragments.add(discoverFragment);
        fragments.add(podcastFragment);
        fragments.add(mineFragment);
        fragments.add(singFragment);
        fragments.add(villageFragment);
    }

    /*
    * 设置首页最小的播放音乐的界面的透明的
    * 下载面的主菜单向下移动
    * */
    public void setMiniPlayerAlphaProgress(float progress){
        float alpha=1-progress;
        miniPlayerFragment.getView().setAlpha(alpha);
        if (miniPlayerFragment.getView().getAlpha()==0f){
            miniPlayerFragment.getView().setVisibility(View.GONE);
            viewPager2.setUserInputEnabled(false); //true:滑动，false：禁止滑动
        }else {
            miniPlayerFragment.getView().setVisibility(View.VISIBLE);
            viewPager2.setUserInputEnabled(true); //true:滑动，false：禁止滑动
        }
        bottomNavigationView.setTranslationY(progress*500);
        bottomNavigationView.setAlpha(alpha);
    }

    public void initFragment(){
        viewPager2=findViewById(R.id.mainViewPager);
        frameLayout=findViewById(R.id.main_sheet);
        bottomNavigationView=findViewById(R.id.bottomNavigationView);
        miniPlayerFragment= (MiniPlayerFragment) getSupportFragmentManager().findFragmentById(R.id.mini_player_fragment);
        playerFragment=(PlayerFragment)getSupportFragmentManager().findFragmentById(R.id.player_fragment);
    }
    /*
    * 初始化上拉抽屉
    * */
    public void initMainSheet(){
        bottomSheetBehavior=BottomSheetBehavior.from(frameLayout);
        bottomSheetCallback=
                new BottomSheetBehavior.BottomSheetCallback() {
                    @Override
                    public void onStateChanged(@NonNull View bottomSheet, int newState) {

                    }

                    @Override
                    public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                          setMiniPlayerAlphaProgress(slideOffset);
                    }
                };
        bottomSheetBehavior.addBottomSheetCallback(bottomSheetCallback);

    }

    public void setUserInfo(UserInfo userInfo){
        MineFragment mineFragment= (MineFragment) fragments.get(2);
        mineFragment.getMinwUserInfo().setVisibility(View.VISIBLE);
        mineFragment.getLoginBtn().setVisibility(View.GONE);
    }

    public ArrayList<Fragment> getFragments() {
        return fragments;
    }
    public void updatePlayList() {
        MineFragment mineFragment= (MineFragment) fragments.get(2);
        mineFragment.updatePlayList(mainViewModel.getPlaylistDTOS());
    }

    public MainViewModel getMainViewModel() {
        return mainViewModel;
    }
}
